package example.telosys.util.http;


import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.telosys.util.http.HttpClient;
import org.telosys.util.http.HttpClientResponse;

public class HttpClientExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    try
	    {
//	      OriginalHttpClient c = new OriginalHttpClient("http://telosys.ow2.org");
//	      c.connect("GET");
//	      c.displayResponse();
//	      c.disconnect();

	      /*
	      c.connect("POST");
	      c.post("data=Posted request");
	      c.displayResponse();
	      c.disconnect();

	      c.connect("POST");
	      c.post("data=2nd request");
	      c.displayResponse();
	      c.disconnect();
	      */
	    	
		  //HttpClient.configProxy("proxyitx.idf.fr.ad.sotranet.net", "8080");
		  System.out.println("Follow redirects ? " + HttpClient.getConfigFollowRedirects() ) ;
		  HttpClient.configFollowRedirects(false);
		  System.out.println("Follow redirects ? " + HttpClient.getConfigFollowRedirects() ) ;
		  
	      HttpClient httpClient = new HttpClient();
	      httpClient.setFollowRedirects(false);
	      
		  System.out.println("Before http GET  " ) ;
	      HttpClientResponse r = httpClient.get("http://www.telosys.org");
		  System.out.println("After http GET  " ) ;
	      
	      System.out.println("Status  : " + r.getStatusCode() );
	      System.out.println("Message : " + r.getStatusMessage() );
	      
	      System.out.println("Content : ");
	      System.out.println(" . length   = " + r.getContentLength() );
	      System.out.println(" . type     = " + r.getContentType());
	      System.out.println(" . encoding = " + r.getContentEncoding() );
	      
	      System.out.println("--- RESPONSE HEADERS ---");
	      Map<String,List<String>> m = r.getHeaderMap();
	      Set<String> keys = m.keySet();
	      for ( String k : keys )
	      {
	    	  System.out.println( " . " + k + " " + m.get(k) );
	      }
	      
	      Collection<String> values = r.getHeader("Server");
	      System.out.println("Server : " + values );
	      // System.out.println("Header class = " + values.getClass() );
	      
	      System.out.println("--- RESPONSE BODY ---");
	      System.out.write(r.getBodyContent());
	      
	    }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	    }
	}

}
